<?php
/**
 * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */
$id = JRequest::getInt('id', 0);
$share = JRequest::getString('share', '');
$userid = JRequest::getString('userid', '');
$type = JRequest::getString('type','');
$shareEvent = PCalendarHelper::getShareEvent($share, $userid);

// If share, allow preview
if ( !$shareEvent ) {
   if( PUtil::checkUserLogin() !== true ) {
      exit;
   }
}

OCP\JSON::checkAppEnabled('calendar');
$object_model = new PCalendarModelObject();

$master_uid = 0;
if (isset($shareEvent['uid_owner'])) {
   if (is_numeric($shareEvent['uid_owner'])) {
      $master_uid = $shareEvent['uid_owner'];
   } else {
      $master_uid = PCompanyHelper::getSubUserMaster($shareEvent['uid_owner']);
   }
}

$preview_info = $object_model->getEventPreviewInfo($id,$master_uid);

if ( !$preview_info )
   exit('No events found.');

$parent_id = isset($preview_info['objectid']) ? $preview_info['objectid'] : $id;

$objecttype = $preview_info['objecttype'];
if ( $objecttype == 'VEVENT' ) {
   $interviewers = isset($preview_info['interviewers']) ? $preview_info['interviewers'] : array();
   $sub_date = isset($preview_info['sub_date']) ? $preview_info['sub_date'] : array();
}
if ( $objecttype == 'VEVENT' && $shareEvent ) {
   // If share, get shared resume link
   $resume_link = PCalendarHelper::getResumeShareLink($preview_info['jobseekerid'], $userid);
}

if( $preview_info['round'] == 1 ){
   $preview_info['round'] = '初试';
} elseif ( $preview_info['round'] > 1 ) {
   $preview_info['round'] = '第'.($preview_info['round']).'轮面试';
} else {
   $preview_info['round'] = '初试';
}

$allowedit = $object_model->checkObjectEditPermission($parent_id);
$user = PFactory::getUser();

$tmpl = new OCP\Template('calendar', 'part.previewevent');
$tmpl->assign('eventid', $parent_id);
$tmpl->assign('objecttype', $objecttype);
$tmpl->assign('preview_info', $preview_info);
$tmpl->assign('allowedit', $allowedit);
$tmpl->assign('user', $user);
$tmpl->assign('type', $type);

if ( $objecttype == 'VEVENT' ) { // Master event
   $tmpl->assign('interviewers', $interviewers);
   $tmpl->assign('sub_date', $sub_date); // It's a subevent
}
if ( $objecttype == 'VEVENT' && $shareEvent ) { // Shared event needs resume's share link
   $tmpl->assign('resume_link', $resume_link);
   // It's a public share event we can simply gen his feedback url
   $feedback_link = PCalendarHelper::getPublicShareLink('event',$share,$userid).'&feedback=1';
   $tmpl->assign('userid', $userid);
   $tmpl->assign('feedback_link', $feedback_link);
}
$tmpl->printpage();